package pri.hillchen.springbootstd.sso.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author:hillchen
 * @DESCRIPTION:
 * @Date: Created in 10:30 2017/12/13 0013.
 */
public class SpringMvcRequestUtil {
    public static final String HEAD_TOKEN = "token";
    public static final String HEAD_SYS= "system";

    public static String getRequestHeader(String headerName){
        HttpServletRequest request = getCurrentRequest();
        return request.getHeader(headerName);
    }

    public static HttpServletRequest getCurrentRequest(){
        ServletRequestAttributes ra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if(ra != null){
            return ra.getRequest();
        }else{
            throw new RuntimeException("没有http request 请求！");
        }
    }

    public static String getCurrentToken(){
        return getRequestHeader(HEAD_TOKEN);
    }

    public static String getCurrentRequestSys(){
        return getRequestHeader(HEAD_SYS);
    }
}
